package org.yelongframework.baidu.aip.ocr.result.location;

import java.util.List;
import java.util.stream.Collectors;

import org.yelongframework.baidu.aip.ocr.result.OcrAipResult;
import org.yelongframework.baidu.aip.ocr.result.OcrAipResultWordsResult;
import org.yelongframework.baidu.aip.ocr.result.OcrAipResultWordsResultLocation;

/**
 * 默认实现
 * 
 * @since 1.0.0
 */
public class DefaultOcrResultLocationScopeSearcher implements OcrResultLocationScopeSearcher {

	// ==================================================检索==================================================

	@Override
	public List<OcrAipResultWordsResult> search(OcrAipResult ocrAipResult, int minTop, int maxTop, int minLeft,
			int maxLeft, int minWidth, int maxWidth, int minHeight, int maxHeight) {
		List<OcrAipResultWordsResult> wordsResult = ocrAipResult.getWordsResult();
		return wordsResult.stream().filter(x -> {
			OcrAipResultWordsResultLocation location = x.getLocation();
			Integer top = location.getTop();
			if (!(top >= minTop && top <= maxTop)) {
				return false;
			}
			Integer left = location.getLeft();
			if (!(left >= minLeft && left <= maxLeft)) {
				return false;
			}
			Integer width = location.getWidth();
			if (!(width >= minWidth && width <= maxWidth)) {
				return false;
			}
			Integer height = location.getHeight();
			if (!(height >= minHeight && height <= maxHeight)) {
				return false;
			}
			return true;
		}).collect(Collectors.toList());
	}

}
